﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.IO;

namespace MasterPageGroupF3
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        String uploadDirectory = "";        
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["idCity"] = 2;
            if (Request.QueryString["logout"] != null)
            {
                if (Request.QueryString["logout"].ToString().Equals("true"))
                {
                    Session.Remove("UserName");
                    Session.Remove("Provider");
                }
            }

            
            
            if (Session["Provider"] != null)
            {
                Label lbTaiKhoan = (Label)this.Master.FindControl("lbTaiKhoan");
                lbTaiKhoan.Text = Session["Provider"].ToString();
                Label lbLogout = (Label)this.Master.FindControl("lbLogout");
                lbLogout.Text = "<li><img src=\"../skin/frontend/default/inspire-blue/images/citydivider.jpg\"/></li>" +
                    "<li class=\"first\"><a href=\"index.aspx?logout=true\">Thoát</a></li>";
                Label lbDangKy = (Label)this.Master.FindControl("lbDangKy");
                lbDangKy.Text = "";
            }

            if (!IsPostBack)
            {
                Label lbRightCol = (Label)Master.FindControl("lbRightCol");
                lbRightCol.Text = "";
                
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            PRODUCT pro = new PRODUCT();
            pro.IDCITY = int.Parse(City.SelectedValue);
            pro.IDPROVIDER = PROVIDERBUS.LayProviderCoUser((String)Session["Provider"]).IDPROVIDER;
            pro.PRICE = int.Parse(GiaGoc.Text);
            pro.RATE_DECREASE = int.Parse(TiLeGiam.Text);
            pro.TIME_START = DateTime.ParseExact(TGBatDau.Text,"dd/mm/yyyy",null);
            pro.TIME_STOP = DateTime.ParseExact(TGKetThuc.Text,"dd/mm/yyyy",null);
            pro.MAX_NUMBER = int.Parse(SLToiDa.Text);
            pro.MIN_NUMBER = int.Parse(SLToiThieu.Text);
            pro.CONDITION = DieuKien.Text;
            pro.HIGHLIGHT = DiemNoiBat.Text;
            pro.CONTENT = ThongTinSP.Text;
            pro.SLOGAN = TenSP .Text;
            pro.STATUS = 0; // trang thai chua xet duyet
            PRODUCTBUS.Add_product(pro);


            Response.Write("<script language='javascript'>alert('Đăng tin thành công. Vui lòng chờ admin xác nhận');</script>");
            Response.Redirect("~/ho-chi-minh/index.aspx");

        }

        protected void txtTenThanhVien_TextChanged(object sender, EventArgs e)
        {

        }
        
        //click vao button upload
        protected void Button1_Click(object sender, EventArgs e)
        {
            // Check that a file is actually being submitted. 
            if (FileUpload.PostedFile.FileName == "")
            {
                lb_thongbao.Text = "No file specified.";
            }
            else
            {
                // Check the extension. 
                string extension = Path.GetExtension(FileUpload.PostedFile.FileName);

                switch (extension.ToLower())
                {
                    case ".bmp":
                    case ".gif":
                    case ".jpg":
                        break;
                    default:
                        lb_thongbao.Text = "This file type is not allowed.";
                        return;
                }
            }
            // Using this code, the saved file will retain its original 
            // file name when it's placed on the server. 
            PRODUCT[] listpro = PRODUCTBUS.LayBang();
            int lastindex = listpro.Count();
            // uploadDirectory =@"..\images\product\" + (lastindex + 1).ToString();
            uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, @"ho-chi-minh\images\product");
            uploadDirectory += @"\" + (lastindex + 1).ToString();
            Directory.CreateDirectory(uploadDirectory);
            string serverFileName = Path.GetFileName(FileUpload.PostedFile.FileName);
            string fullUploadPath = Path.Combine(uploadDirectory,serverFileName);

            try
            {
                FileUpload.PostedFile.SaveAs(fullUploadPath);
                lb_thongbao.Text = "Upload successfully";

            }
            catch (Exception err)
            {
                lb_thongbao.Text = err.Message;
            }
        }

        protected void ThongTinSP_TextChanged(object sender, EventArgs e)
        {

        }

       
    }
}